package com.sskj.app.presenter;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.App;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.util.DisposUtil;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.bean.AppVersionBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.GonggaoBean;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.util.APKVersionCodeUtils;
import com.sskj.app.ui.activity.MainActivity;

import io.reactivex.disposables.Disposable;


public class MainActivityPresenter extends BasePresenter<MainActivity> {

    private WebSocketObserver stockSocket;
    private Disposable stockDispo;
    private WebSocketObserver stockSocketLevel;
    private Disposable stockDispoLevel;


    public void getGonggao() {
        httpService.getGonggao()
                .execute(new CallBackOption<HttpData<GonggaoBean>>() {
                }
                        .unLoadBind(mView)
                        .execute(httpData -> mView.showGonggao(httpData.getData())));
    }


    public void initWebSocket() {
        if (stockSocket == null) {
            stockSocket = httpService.pushCoinType(true);
            stockDispo = stockSocket

                    .map(s -> new Gson().fromJson(s, CoinBean.class))
                    .subscribe(coinBean -> LiveDataBus.get().with(RxBusCode.PUSH_COIN_BEAN_BIBI, CoinBean.class)
                            .postValue(coinBean), e -> {
                    });
            stockSocketLevel = httpService.pushCoinType(false);
            stockDispoLevel = stockSocketLevel
                    .map(s -> new Gson().fromJson(s, CoinBean.class))
                    .subscribe(coinBean -> {
                        LiveDataBus.get().with(RxBusCode.PUSH_COIN_BEAN_LEVEL, CoinBean.class)
                                .postValue(coinBean);
                        LiveDataBus.get().with(RxBusCode.PUSH_COIN_BEAN_BIBI, CoinBean.class)
                                .postValue(coinBean);
                    }, e -> {
                    });
        }

    }

    @Override
    public void detachView() {
        DisposUtil.close(stockDispo);
        DisposUtil.close(stockDispoLevel);
        if (stockSocket != null) {
            stockSocket.close();
            stockSocket = null;
            stockSocketLevel.close();
            stockSocketLevel = null;
        }
        super.detachView();
    }


    public void getNewVersion() {
        httpService.getVersion()
                .execute(new CallBackOption<HttpData<AppVersionBean>>() {
                    @Override
                    public void onError(Response<HttpData<AppVersionBean>> response) {
                        getGonggao();
                        super.onError(response);
                    }
                }.unLoadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getData() != null) {
                                String verName = APKVersionCodeUtils.getVerName(App.INSTANCE);
                                if (APKVersionCodeUtils.compareVersion(httpData.getData().getAppVersion(), verName) == 1) {
                                    mView.onVersionDataSuccess(httpData.getData());
                                } else {
                                    getGonggao();
                                }
                            } else {
                                getGonggao();
                            }
                        }));
    }
}
